import ConfigParser
from utils import *
import os

CONFIG_FILE = ""
CONFIG_APPS_DIR_DEFAULT = 'apps'

class config:
    """class holding main application conf"""
    def __init__(self,configFile):
        self._config = ConfigParser.ConfigParser()
        self._configfile = configFile 
        self._config.read(self._configfile) 
        
    def getSetting(self,section,option):
        try:
            ret = self._config.get(section,option)
        except:
            ret = None
        infoOutput('Config : Get setting %s[%s] : %s'%(section,option,ret))
        return ret
    
    def setSetting(self,section,option,value):
        if not self._config.has_section(section):
            self._config.add_section(section)
        try:
            self._config.set(section,option,value)
            with open(self._configfile, 'wb') as configfile:
                self._config.write(configfile)
            infoOutput('Config : Set setting %s[%s] : %s'%(section,option,value))
            return True
        except:
            return False
        
    def clearSection(self,section):
        try:
            self._config.remove_section(section)
            with open(self._configfile, 'wb') as configfile:
                self._config.write(configfile)
            infoOutput('Config : Clear settings section : %s'%(section))
            return True
        except:
            return False

        

def getSetting(section,option):
    """function to directly get settings from main configfile"""
    global CONFIG_FILE
    settings = config(CONFIG_FILE)
    return settings.getSetting(section,option)

def clearSectionSetting(section):
    """function to directly clear settings section from main configfile"""
    global CONFIG_FILE
    settings = config(CONFIG_FILE)
    return settings.clearSection(section)

def setSetting(section,option,value):
    """function to directly set settings from main configfile"""
    global CONFIG_FILE
    settings = config(CONFIG_FILE)
    return settings.setSetting(section,option,value)